import requests,pytest
from faker import Faker
project_url = "https://jxc.tao51d.com"
faker = Faker(locale='zh_cn')
name = faker.name()
id_list = []
class TestBuer:
    @pytest.fixture(scope='function',autouse=True)
    def setup_teardown(self):
        response = requests.post(project_url + "/api/oauth/token?grant_type=password",headers= {"Authorization":
                                                                                         "Basic YnVlcnl1bl9wYzpidWVyeXVuX3Bj","Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
                                 data = {"username":"13840103191","password":"fdy211382"})

        self.access_token = response.json()['access_token']

    def test_add(self):
        response = requests.post(project_url +"/api/goodCat/saveData",headers={"Authorization": "Bearer " + str(self.access_token),"Content-Type":"application/json; charset=UTF-8"},json={"id":"null","pid":"1697181172041383937","name":str(name)})
        assert response.json()['title'] =="保存成功！"
        assert response.status_code == 200
        id_list.append(response.json()['data']['id'])

    def test_delete(self):
        response = requests.post(project_url +"/api/goodCat/delete",headers={"Authorization": "Bearer " + str(self.access_token),"Content-Type":"application/json; charset=UTF-8"},json=int(id_list[0]),
                      )
        assert response.json()['title'] == "成功"
        assert response.status_code == 200
